博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Strut2中的session和servlet中的session的区别
阅读量:7014 次
发布时间:2019-06-28

本文共 4545 字,大约阅读时间需要 15 分钟。

 

在jsp中,内通过内置对象 HttpServletRequest的getSession()方法可以获取到HttpSession,比如:

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <%@taglib uri="http://www.wyl.suneyaee" prefix="Wyl"%> 4 
5 <%@ include file="/WEB-INF/head.jsp"%> 6 7 8 9 10
11 39 40 Insert title here41 42 43
44 45
46
47 48
49
51
52 53
54 用户名55
56
57 <%58 request.getSession().setAttribute("hahaAnyKey", "我是hahaAnyKey.jsp页面传入session的值");59 %>60
61
62 63 64
65 用户名2 ,模型驱动,而且这个表单对应的LoginAction2.java中封装了dto,而且可以使用Delete键清空输入框66
68
69
70 71 -----------72 74 75 76

中的58行,就可以获取到session,然后直接在HttpSession里存放一个值。

而在Struts2中,比如在一个action中,可以直接通过ActionContext.getContext().getSession()获取Session对象(注意:这个ActionContext是

. . .ActionContext类型的),实际上这里的Session是一个Mapper,只是Struts2做了封装,我们还是可以通过这个mapper类型的Mapper来获取到上面登录页面放在HttpSession中的属性。
比如在一个action中就可以通过如下代码获取到上面jsp中存放在HttpSession中的属性,
1 ActionContext atx = ActionContext.getContext();2             Map
session = atx.getSession();3 Set
set = session.keySet();4 Iterator it = set.iterator();5 while(it.hasNext()){6 String key = (String) it.next();7 String val = (String) session.get(key);8 System.out.println("key:"+key+",value:"+val);9 }

打印结果:

key:hahaAnyKey,value:我是hahaAnyKey.jsp页面传入session的值

这个Action的完整代码如下:

1 package com.log; 2  3 import java.io.PrintWriter; 4 import java.util.Iterator; 5 import java.util.Map; 6 import java.util.Set; 7  8 import javax.servlet.http.HttpServletResponse; 9 10 import org.apache.struts2.ServletActionContext;11 12 import com.log.entity.User;13 import com.log.service.UserService;14 import com.opensymphony.xwork2.ActionContext;15 import com.opensymphony.xwork2.ActionSupport;16 import com.util.UtilTime;17 18 /**19  * 普通的登陆20  * 21  * @author Wei22  * 23  */24 public class LoginAction extends ActionSupport {25     /**26      * 27      */28     private static final long serialVersionUID = 1L;29     private User user;30     private UserService uService = new UserService();31     private String error;32 33     public String getEror() {34         return error;35     }36 37     public void setEror(String error) {38         this.error = error;39     }40 41     public UserService getuService() {42         return uService;43     }44 45     public void setuService(UserService uService) {46         this.uService = uService;47     }48 49     public User getUser() {50         return user;51     }52 53     public void setUser(User user) {54         this.user = user;55     }56 57     @Override58     public String execute() throws Exception {59         /**60          * 账号密码:wyl,123461          */62         if (uService.login(user)) {63             ActionContext atx = ActionContext.getContext();64             Map
session = atx.getSession();65 //对应于 WylInterceptor.java拦截器,这个拦截器里需要用到66 session.put("currentUser", user);67 //用来设置登录超时的参数68 session.put("lastTime", UtilTime.getCurTime());69 return SUCCESS;70 } else {71 // this.error = "error";72 // return this.error;73 HttpServletResponse resp = ServletActionContext.getResponse();74 //不加 resp.setContentType("text/html;charset=UTF-8"); 会出现页面的中文为乱码的情况75 resp.setContentType("text/html;charset=UTF-8");76 PrintWriter out = resp.getWriter();77 out.println("账号:"+user.getUserName()+",
密码:"+user.getPassWord()+" 的不正确,请确认");78 // out.println("
");79 out.println("
点我重新登陆");80 /**81 * 下面的Map
session = atx.getSession();为了演示获取到的session(实际上是Mapper,Struts2框架做了封装)82 */83 ActionContext atx = ActionContext.getContext();84 Map
session = atx.getSession();85 Set
set = session.keySet();86 Iterator it = set.iterator();87 while(it.hasNext()){88 String key = (String) it.next();89 String val = (String) session.get(key);90 System.out.println("key:"+key+",value:"+val);91 }92 //什么都不返回,直接用HttpServletResponse在网页上写提示93 return null;94 }95 }96 }

,调试截图:

 

 

 

转载地址:http://qiqtl.baihongyu.com/

你可能感兴趣的文章
「每日一码」(精品代码,质量保证)阶乘
查看>>
python日常使用
查看>>
进位标志位
查看>>
Solr与Elasticsearch比较
查看>>
缓存穿透和雪崩
查看>>
ACCESS_ONCE
查看>>
c++11特性使用
查看>>
setTimeout使用闭包功能,实现定时打印数值
查看>>
【单调队列】滑动窗口
查看>>
C# Redis使用之StackExchange
查看>>
FortiGate安全策略说明
查看>>
HDU3127 WHUgirls
查看>>
4、客户机策略配置
查看>>
memcached的图形界面监控
查看>>
Air Raid
查看>>
常用的Windows命令
查看>>
Spring 教程(二) 体系结构
查看>>
第18条:尽量使用不可变对象
查看>>
点滴积累【other】---HTTP Error 503. The service is unavailable (转载)
查看>>
sqlite数据库查看操作
查看>>